%close all;
clear all;

Fs=50e6;
NoofSamples=512;
FFTsize=2*NoofSamples;
NoofFFTCores=1024;
%FreqinOneBin=Fs/FFTsize;
A=1.0;
RampaSayisi=2;
complex_signal=1;
% For dongusu ile degistirilecekler
SNR_dB=-5; %dB;
fc=Fs/4;
alpha=30:5:60;
theta=alpha; %90-alpha;
%BW=FreqinOneBin*32;

lengththeta = length(theta);
disp('Creating Hough Transform Space');
thresh = 0.40;  % Threshold for Wigner Image.

for sigNum = 1: lengththeta
	%Sinyalin chirp degerine gore klasorun ilk isminin belirlenmesi
	txt0 = ['D:\Tez_mat_dosyalari\FMCW_WDS\WD_S',num2str(theta(sigNum)),'_100\SNR_'];
	txt3 = ['D:\Tez_mat_dosyalari\FMCW_WHTS\WHT_S',num2str(theta(sigNum)),'_100\SNR_'];
	
    for SNR_dB=-3:-2:-9
	% Klasorun ilk ismine sinyalin SNR_dB degerine gore ek yapilmasi
        txtReadFolder = [txt0 num2str(SNR_dB) 'dB'];
		txtWriteFolder = [txt3 num2str(SNR_dB) 'dB'];
        mkdir(txtWriteFolder);
		dWD = dir(txtReadFolder);
		ltWD = length(dWD);
		
        for rnloopidx=3:(ltWD) % From 3 onwards to avoid directories
			disp(['HT Generation for Signum:' num2str(sigNum) ' SNR_dB:' num2str(SNR_dB) ' rnloopidx:' num2str(rnloopidx)]);
			% Reading the test signal
			txt2 = [txtReadFolder '\' dWD(rnloopidx).name];
			load(txt2)

            wig = real_Sups;
            Th2 = thresh*max(max(wig));
            [fr,fcol] = size(wig);
            hippo = floor(sqrt(fcol^2+fr^2));
            
            %% FAST HOUGH TRANSFORM
            Hough=zeros(hippo,71);
            for i=1:fcol
                for j=1:fr
                    if wig(j,i)<=Th2
                        for theta=1:71
                            r=round(i*cos((theta+9)*pi/180)+j*sin((theta+9)*pi/180));
                            if r<1
                                r=1;
                                Hough(r,theta)=Hough(r,theta);
                            else
                                Hough(r,theta)=Hough(r,theta)+1;
                            end
                        end
                    end
                end
            end
            txt4 = [txt3 num2str(SNR_cnt) 'dB\' dWD(i1).name];
            save(txt4,'Hough');
        end
    end
end
